package com.example.hyw.szysteam.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.hyw.szysteam.POJO.Practice;
import com.example.hyw.szysteam.R;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/6/14.
 */

public class PracticeAdapter extends ArrayAdapter<Practice> {
    private int resource;
    private ArrayList<Practice> practices;

    public PracticeAdapter(@NonNull Context context, int resource, ArrayList<Practice> practices) {
        super(context, resource, practices);
        this.resource=resource;
        this.practices = practices;
    }

    @Nullable
    @Override
    public Practice getItem(int position) {
        return practices.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Practice practice=getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView==null){
            view= LayoutInflater.from(getContext()).inflate(resource,null);
            viewHolder=new ViewHolder();
            viewHolder.question=view.findViewById(R.id.practice_question);
            viewHolder.answer=view.findViewById(R.id.practice_answer);
            view.setTag(viewHolder);
        }else{
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.question.setText(practice.getQuestion());
        viewHolder.answer.setText(practice.getAnswer());
        return view;
    }

    class ViewHolder{
        TextView question;
        TextView answer;
    }
}
